﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.hud.components {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import flash.utils.*;
    import org.aswing.*;
    import Classes.*;
    import Classes.util.*;
    import com.greensock.*;
    import flash.filters.*;
    import org.aswing.geom.*;
    import Display.*;
    import flash.text.*;
    import Display.aswingui.*;
    import Modules.stats.types.*;
    import L10n.*;
    import Display.hud.*;

    public class HUDEnergyComponent extends HUDComponent {

        private static var m_firstInit:Boolean = true;

        public var m_energySliderAsset:Class;
        protected var m_head:EnergyHead;
        private var m_energyHolder:JPanel;
        public var m_energyText:JTextField;
        public var m_energyMoreText:JTextField;
        private var m_bEnergyWindowDown:Boolean = false;
        private var m_moreEnergyWindow:JWindow;
        public var m_energySlider:HUDEnergyComponent_m_energySliderAsset;
        private var m_energyMoreTimer:Timer;

        public function HUDEnergyComponent(_arg1:Function=null){
            this.m_energySliderAsset = HUDEnergyComponent_m_energySliderAsset;
            this.m_energySlider = (new HUDEnergyComponent_m_energySliderAsset() as HUDEnergyComponent_m_energySliderAsset);
            super();
        }
        override protected function buildComponent():void{
            m_jPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER, 0);
            var _local1:Sprite = new Sprite();
            addChild(_local1);
            this.m_moreEnergyWindow = new JWindow(_local1);
            var _local2:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.BOTTOM);
            var _local3:DisplayObject = (new EmbeddedArt.getMoreEnergyPanel() as DisplayObject);
            ASwingHelper.setSizedBackground(_local2, _local3);
            this.m_moreEnergyWindow.setContentPane(_local2);
            ASwingHelper.prepare(this.m_moreEnergyWindow);
            this.m_moreEnergyWindow.setX(30);
            this.m_moreEnergyWindow.setY(-10);
            this.m_moreEnergyWindow.show();
            var _local4:Sprite = new Sprite();
            addChild(_local4);
            _local4.graphics.beginFill(0);
            _local4.graphics.drawRect(30, 35, _local3.width, 50);
            _local4.graphics.endFill();
            _local1.mask = _local4;
            var _local5:TextFormat = new TextFormat(EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 18, 0xFFFFFF, true, false, false, null, null, TextFormatAlign.LEFT);
            var _local6:TextFormat = new TextFormat(EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 11, 0xFFFFFF, true, false, false, null, null, TextFormatAlign.LEFT);
            var _local7:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            this.m_energyMoreText = ASwingHelper.makeTextField(" ", EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 11, EmbeddedArt.brownTextColor, 0, _local6);
            _local7.setPreferredWidth(120);
            _local7.appendAll(ASwingHelper.horizontalStrut(18), this.m_energyMoreText);
            this.m_energyHolder = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.BOTTOM, -5);
            this.m_energyHolder.setPreferredWidth(120);
            var _local8:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            this.m_energyText = ASwingHelper.makeTextField("0", EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 18, 0xFFFFFF, 0, _local5);
            LocaleHelper.localSetting.ASwingHelper_setEasyBorder(this.m_energyText, 0, 0, 0, (this.m_energyText.width + 6));
            this.m_energyText.filters = [new GlowFilter(0, 0.5, 3, 3, 10)];
            _local8.append(this.m_energyText);
            this.m_energyHolder.appendAll(_local7, _local8, ASwingHelper.verticalStrut(23));
            var _local9:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.LEFT);
            _local9.setBackgroundDecorator(new MarginBackground(this.m_energySlider, new Insets(8)));
            _local9.setPreferredSize(new IntDimension((this.m_energySlider.width + 5), (this.m_energySlider.height + 8)));
            _local9.setMaximumSize(new IntDimension((this.m_energySlider.width + 5), (this.m_energySlider.height + 8)));
            _local9.setMinimumSize(new IntDimension((this.m_energySlider.width + 5), (this.m_energySlider.height + 8)));
            _local9.appendAll(ASwingHelper.horizontalStrut(50), this.m_energyHolder);
            m_jPanel.append(_local9);
        }
        override public function refresh(_arg1:Boolean):void{
            super.refresh(_arg1);
            this.m_energySlider.scaleX = 1;
        }
        public function moveHead(_arg1:String):void{
            if (this.m_head != null){
                this.m_head.goToFrame(_arg1);
            };
        }
        override protected function attachToolTip():void{
            m_toolTip = new ToolTip(function ():String{
                return (ZLoc.t("Main", "Energy_ToolTip", {
                    energy:m_energyText.getText(),
                    maxenergy:String(Global.player.energyMax),
                    message:m_energyMoreText.getText()
                }));
            });
            m_toolTip.attachToolTip(this);
            m_toolTip.hideCursor = true;
        }
        override protected function setCounter():void{
            m_counter = new HUDCounter(function (_arg1:Number):void{
                var _local2:* = int(m_energyText.getText());
                if (_arg1 == 0){
                    moveHead(EnergyHead.NO_ENERGY);
                } else {
                    if (_arg1 == Global.player.energyMax){
                    } else {
                        if (_arg1 > _local2){
                            moveHead(EnergyHead.GAIN_ENERGY);
                        } else {
                            moveHead(EnergyHead.LOSE_ENERGY);
                        };
                    };
                };
                if (m_moreEnergyWindow){
                    if (_arg1 <= Global.gameSettings.getInt("energyWarningAmount")){
                        m_bEnergyWindowDown = true;
                        m_moreEnergyWindow.addEventListener(MouseEvent.CLICK, onMoreEnergyClick, false, 0, true);
                        TweenLite.to(m_moreEnergyWindow, 0.5, {y:30});
                    } else {
                        m_bEnergyWindowDown = false;
                        m_moreEnergyWindow.removeEventListener(MouseEvent.CLICK, onMoreEnergyClick);
                        TweenLite.to(m_moreEnergyWindow, 0.5, {y:-10});
                    };
                };
                m_energyText.setText(String(_arg1));
                calcSliderSize(_arg1);
            });
            m_counter.value = Global.player.energy;
            this.refresh(false);
            this.m_energyMoreTimer = new Timer(1000);
            this.m_energyMoreTimer.addEventListener(TimerEvent.TIMER, this.updateEnergyCounter);
            this.m_energyMoreTimer.start();
        }
        private function calcSliderSize(_arg1:Number):void{
            var _local4:Number;
            var _local6:TextFormat;
            var _local2 = 35;
            var _local3:Number = Global.player.energyMax;
            var _local5:MovieClip = (this.m_energySlider.sliderMask as MovieClip);
            if (_arg1 > Global.player.energyMax){
                _local6 = new TextFormat(EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 18, 0xFF00, true, false, false, null, null, TextFormatAlign.LEFT);
                this.m_energyText.setTextFormat(_local6);
                _local4 = 1;
            } else {
                _local6 = new TextFormat(EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 18, 0xFFFFFF, true, false, false, null, null, TextFormatAlign.LEFT);
                this.m_energyText.setTextFormat(_local6);
                _local4 = (_arg1 / _local3);
            };
            _local5.x = (((_local4 - 1) * _local5.width) + _local2);
        }
        public function updateEnergyCounter(_arg1:TimerEvent):void{
            var _local3:int;
            var _local4:int;
            var _local5:int;
            var _local6:String;
            var _local2:Player = Global.player;
            if (_local2.energy < _local2.energyMax){
                _local3 = Global.gameSettings.getNumber("energyRegenerationSeconds");
                _local4 = int((Math.floor((GlobalEngine.getTimer() / 1000)) - Global.player.lastEnergyCheck));
                _local5 = (_local3 - _local4);
                if (_local5 < 0){
                    _local5 = (_local5 + _local3);
                    _local2.regenerateEnergy();
                };
                _local6 = GameUtil.formatMinutesSeconds(_local5);
                if (_local6.charAt(0) == "0"){
                    _local6 = _local6.slice(1);
                };
                this.m_energyMoreText.setText(ZLoc.t("Main", "EnergyCounter", {time:_local6}));
                if (this.m_energyMoreText.getText() == ""){
                    this.m_energyMoreText.setText(" ");
                };
                this.m_energyMoreText.visible = true;
                this.calcSliderSize(_local2.energy);
            } else {
                this.m_energyMoreText.visible = false;
                this.m_energyMoreText.setText("");
            };
        }
        private function onMoreEnergyClick(_arg1:MouseEvent):void{
            UI.displayCatalog("extras", "energy_2", false, "", false, false, true);
            StatsManager.sample(100, StatsCounterType.HUD_COUNTER, "add_energy");
        }

    }
}//package Display.hud.components 
